package zhjb.platform.util;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * 读取配置文件中的配置项
 * 
 * @author zhangjianbing@msn.com
 * 
 */
public class AppConf {
	/**
	 * 保存所有的配置属性值
	 */
	private static Map<String, String> values = new HashMap<String, String>();

	/**
	 * 初始化
	 */
	static {
		synchronized (AppConf.class) {
			init();
		}
	}

	/**
	 * 初始化
	 */
	private static void init() {
		synchronized (values) {
			values.clear();
			URL url = Thread.currentThread().getContextClassLoader().getResource("config.xml");
			if (url != null) {
				DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
				try {
					DocumentBuilder builder = dbf.newDocumentBuilder();
					Document doc = builder.parse(url.openStream());
					Node root = doc.getFirstChild();
					NodeList list = root.getChildNodes();
					int length = list.getLength();
					if (length > 0) {
						for (int i = 0; i < length; i++) {
							Node node = list.item(i);
							NodeList children = node.getChildNodes();
							String key = null;
							String value = null;
							int clength = children.getLength();
							for (int k = 0; k < clength; k++) {
								Node cNode = children.item(k);
								if ("name".equalsIgnoreCase(cNode.getNodeName())) {
									key = trim(cNode.getTextContent());
								} else if ("value".equalsIgnoreCase(cNode.getNodeName())) {
									value = cNode.getTextContent();
								}
							}
							if (key != null && key.length() > 0) {
								values.put(key, value);
							}
						}
					}
				} catch (ParserConfigurationException e) {
					e.printStackTrace();
				} catch (SAXException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 字符串初始化
	 * 
	 * @param text
	 * @return
	 */
	public static String trim(String text) {
		return text == null ? "" : text.trim();
	}

	/**
	 * 以String的形式获取配置
	 * 
	 * @param key
	 * @return
	 */
	public static String getString(String key) {
		return getString(key, null);
	}

	/**
	 * 以String的形式获取配置
	 * 
	 * @param key
	 *            配置key
	 * @param defaultValue
	 *            找不到的时候的默认值
	 * @return
	 */
	public static String getString(String key, String defaultVal) {
		String result = null;
		if (key != null) {
			result = values.get(key);
		}
		return result == null ? defaultVal : result;
	}

	/**
	 * 以int的形式获取配置
	 * 
	 * @param key
	 *            配置key
	 * @param defaultVal
	 *            找不到的时候的默认值
	 * @return
	 */
	public static int getInt(String key, int defaultVal) {
		int result = defaultVal;
		if (key != null) {
			String value = values.get(key);
			if (value != null) {
				try {
					result = Integer.parseInt(value.trim());
				} catch (Exception e) {
				}
			}
		}
		return result;
	}

	/**
	 * 
	 * @param key
	 * @param defaultVal
	 * @return
	 */
	public static double getDouble(String key, double defaultVal) {
		double result = defaultVal;
		if (key != null) {
			String value = values.get(key);
			if (value != null) {
				try {
					result = Double.parseDouble(value.trim());
				} catch (Exception e) {
				}
			}
		}
		return result;
	}

	/**
	 * 
	 * @param key
	 * @param defaultVal
	 * @return
	 */
	public static float getFloat(String key, float defaultVal) {
		float result = defaultVal;
		if (key != null) {
			String value = values.get(key);
			if (value != null) {
				try {
					result = Float.parseFloat(value.trim());
				} catch (Exception e) {
				}
			}
		}
		return result;
	}

	/**
	 * 
	 * @param key
	 * @param defaultVal
	 * @return
	 */
	public static long getLong(String key, long defaultVal) {
		long result = defaultVal;
		if (key != null) {
			String value = values.get(key);
			if (value != null) {
				try {
					result = Long.parseLong(value.trim());
				} catch (Exception e) {
				}
			}
		}
		return result;
	}
}
